﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace VietThinks.Web.Mvc
{
    public static class DataListExtension
    {
        public static IList<TModel> ToVerticalRepeatingList<TModel>(this IList<TModel> items, int numOfColumns)
        {
            var newList = new List<TModel>();
            
            int numOfRows = items.Count / numOfColumns;
            int remainder = items.Count % numOfColumns;

            for (int row = 0; row <= numOfRows; row++)
            {
                for (int col = 0; col < numOfColumns; col++)
                {                   
                    int index = numOfRows * col + row;

                    if (row < numOfRows || col < remainder)
                    {
                        index += remainder < col ? remainder : col;
                        if (index < items.Count)
                        {
                            newList.Add(items[index]);
                        }
                    }
                }
            }

            return newList;
        }
    }
}
